home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / wnos / wn941101 / iphdr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-10  |  3.9 KB  |  177 lines

  1. /* IP header conversion routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "ip.h"
  7. #include "internet.h"
  8.  
  9. /* Convert IP header in host format to network mbuf
  10.  * If cflag != 0, take checksum from structure,
  11.  * otherwise compute it automatically.
  12.  */
  13. struct mbuf *
  14. htonip(ip,data,cflag)
  15. struct ip *ip;
  16. struct mbuf *data;
  17. int cflag;
  18. {
  19.     int16 hdr_len, fl_offs;
  20.     struct mbuf *bp;
  21.     register char *cp;
  22.  
  23.     hdr_len = IPLEN + ip->optlen;
  24.     if((bp = pushdown(data,hdr_len)) == NULLBUF){
  25.         free_p(data);
  26.         return NULLBUF;
  27.     }
  28.     cp = bp->data;
  29.     
  30.     *cp++ = (ip->version << 4) | (hdr_len >> 2);
  31.     *cp++ = ip->tos;
  32.     cp = put16(cp,ip->length);
  33.     cp = put16(cp,ip->id);
  34.     fl_offs = ip->offset >> 3;
  35.     if(ip->flags.congest)
  36.         fl_offs |= 0x8000;
  37.     if(ip->flags.df)
  38.         fl_offs |= 0x4000;
  39.     if(ip->flags.mf)
  40.         fl_offs |= 0x2000;
  41.  
  42.     cp = put16(cp,fl_offs);
  43.     *cp++ = ip->ttl;
  44.     *cp++ = ip->protocol;
  45.     if(cflag){
  46.         /* Use checksum from host structure */
  47.         cp = put16(cp,ip->checksum);
  48.     } else {
  49.         /* Clear checksum for later recalculation */
  50.         *cp++ = 0;
  51.         *cp++ = 0;
  52.     }
  53.     cp = put32(cp,ip->source);
  54.     cp = put32(cp,ip->dest);
  55.     if(ip->optlen != 0)
  56.         memcpy(cp,ip->options,ip->optlen);
  57.  
  58.     /* If requested, recompute checksum and insert into header */
  59.     if(!cflag)
  60.         put16(&bp->data[10],cksum(NULLHEADER,bp,hdr_len));
  61.  
  62.     return bp;
  63. }
  64.  
  65. /* Extract an IP header from mbuf */
  66. int
  67. ntohip(ip,bpp)
  68. struct ip *ip;
  69. struct mbuf **bpp;
  70. {
  71.     int16 ihl, fl_offs;
  72.     char ipbuf[IPLEN];
  73.  
  74.     if(pullup(bpp,ipbuf,IPLEN) != IPLEN)
  75.         return -1;
  76.  
  77.     ip->version = (ipbuf[0] >> 4) & 0xf;
  78.     ip->tos = ipbuf[1];
  79.     ip->length = get16(&ipbuf[2]);
  80.     ip->id = get16(&ipbuf[4]);
  81.     fl_offs = get16(&ipbuf[6]);
  82.     ip->offset = fl_offs << 3;
  83.     ip->flags.mf = (fl_offs & 0x2000) ? 1 : 0;
  84.     ip->flags.df = (fl_offs & 0x4000) ? 1 : 0;
  85.     ip->flags.congest = (fl_offs & 0x8000) ? 1 : 0;
  86.     ip->ttl = ipbuf[8];
  87.     ip->protocol = ipbuf[9];
  88.     ip->checksum = get16(&ipbuf[10]);
  89.     ip->source = get32(&ipbuf[12]);
  90.     ip->dest = get32(&ipbuf[16]);
  91.  
  92.     ihl = (ipbuf[0] & 0xf) << 2;
  93.     if(ihl < IPLEN){
  94.         /* Bogus packet; header is too short */
  95.         return -1;
  96.     }
  97.     ip->optlen = ihl - IPLEN;
  98.     if(ip->optlen != 0)
  99.         pullup(bpp,ip->options,ip->optlen);
  100.  
  101.     return ip->optlen + IPLEN;
  102. }
  103.  
  104. /* Perform end-around-carry adjustment */
  105. static int16
  106. eac(sum)
  107. int32 sum;    /* Carries in high order 16 bits */
  108. {
  109.     int16 csum;
  110.  
  111.     while((csum = sum >> 16) != 0)
  112.         sum = csum + (sum & 0xffffL);
  113.     return (int16) (sum & 0xffffl);    /* Chops to 16 bits */
  114. }
  115.  
  116. /* Checksum a mbuf chain, with optional pseudo-header */
  117. int16
  118. cksum(ph,m,len)
  119. struct pseudo_header *ph;
  120. register struct mbuf *m;
  121. int16 len;
  122. {
  123.     int swap = 0;
  124.     int16 cnt, total, csum1;
  125.     int32 csum, sum = 0;
  126.     char *up;
  127.  
  128.     /* Sum pseudo-header, if present */
  129.     if(ph != NULLHEADER){
  130.         sum = hiword(ph->source);
  131.         sum += loword(ph->source);
  132.         sum += hiword(ph->dest);
  133.         sum += loword(ph->dest);
  134.         sum += uchar(ph->protocol);
  135.         sum += ph->length;
  136.     }
  137.     /* Now do each mbuf on the chain */
  138.     for(total = 0; m != NULLBUF && total < len; m = m->next) {
  139.         cnt = min(m->cnt, len - total);
  140.         up = (char *)m->data;
  141.         csum = 0;
  142.  
  143.         if(((long)up) & 1){
  144.             /* Handle odd leading byte */
  145.             if(swap)
  146.                 csum = uchar(*up++);
  147.             else
  148.                 csum = (int16)(uchar(*up++) << 8);
  149.             cnt--;
  150.             swap = !swap;
  151.         }
  152.         if(cnt > 1){
  153.             /* Have the primitive checksumming routine do most of
  154.              * the work. At this point, up is guaranteed to be on
  155.              * a short boundary
  156.              */
  157.             csum1 = lcsum((unsigned short *)up, (int16)(cnt >> 1));
  158.             if(swap)
  159.                 csum1 = (csum1 << 8) | (csum1 >> 8);
  160.             csum += csum1;
  161.         }
  162.         /* Handle odd trailing byte */
  163.         if(cnt & 1){
  164.             if(swap)
  165.                 csum += uchar(up[--cnt]);
  166.             else
  167.                 csum += (int16)(uchar(up[--cnt]) << 8);
  168.             swap = !swap;
  169.         }
  170.         sum += csum;
  171.         total += m->cnt;
  172.     }
  173.     /* Do final end-around carry, complement and return */
  174.     return (int16)(~eac(sum) & 0xffff);
  175. }
  176.  
  177.